import React from 'react'
import PropTypes from 'prop-types'
import { Form, Input, Modal, Select, Row, Col, InputNumber } from 'antd'
import { adminCompanyAccoutType, CompanyAccountInOut } from '../../../utils/seletLocalData'

const FormItem = Form.Item
const Option = Select.Option
const { TextArea } = Input

const formItemLayout = {
  labelCol: {
    span: 10,
  },
  wrapperCol: {
    span: 14,
  },
}

const modal = ({
  item = {},
  onOk,
  form: {
    getFieldDecorator,
    validateFields,
    getFieldsValue,
  },
  companyChargeTypeData,
  name,
  ...modalProps
}) => {
  const handleOk = () => {
    validateFields((errors) => {
      if (errors) {
        return
      }
      const data = {
        ...getFieldsValue(),
        id: item.id || 1,
      }

      onOk(data)
    })
  }
  const modalOpts = {
    ...modalProps,
    onOk: handleOk,
  }
  const accountTypeOption = adminCompanyAccoutType.map(d => <Option key={d.key}>{d.value}</Option>)
  const companyChargeTypeOptions = companyChargeTypeData.map(d => <Option key={d.id}>{d.typeName}</Option>)
  const CompanyAccountInOutOptions = CompanyAccountInOut.map(d => <Option key={d.key}>{d.value}</Option>)
  return (
    <Modal {...modalOpts}>
      <Form layout="horizontal">
        <Row>
          <Col span={12}>
            <FormItem label="Account Name" hasFeedback {...formItemLayout}>
              {getFieldDecorator('name', {
                initialValue: name,
                rules: [
                  {
                    required: true,
                  },
                ],
              })(<Input disabled />)}
            </FormItem>
          </Col>

          <Col span={12}>
            <FormItem label="In/Out" hasFeedback {...formItemLayout}>
              {getFieldDecorator('type', {
                initialValue: item.type,
                rules: [
                  {
                    required: true,
                  },
                ],
              })(<Select>
                {CompanyAccountInOutOptions}
              </Select>)}
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span={12}>
            <FormItem label="Amount" hasFeedback {...formItemLayout}>
              {getFieldDecorator('amount', {
                initialValue: item.amount,
                rules: [
                  {
                    required: true,
                  },
                ],
              })(<Input type="number" />)}
            </FormItem>
          </Col>
          <Col span={12}>
            <FormItem label="ChargeType" hasFeedback {...formItemLayout}>
              {getFieldDecorator('chargeTypeId', {
                initialValue: `${item.chargeTypeId}`,
                rules: [
                  {
                    required: true,
                  },
                ],
              })(<Select>
                {companyChargeTypeOptions}
              </Select>)}
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span={12}>
            <FormItem label="Detail" hasFeedback {...formItemLayout}>
              {getFieldDecorator('memo', {
                initialValue: item.memo,
              })(<TextArea />)}
            </FormItem>
          </Col>
        </Row>
      </Form>
    </Modal>
  )
}

modal.propTypes = {
  form: PropTypes.object.isRequired,
  type: PropTypes.string,
  item: PropTypes.object,
  onOk: PropTypes.func,
  companyChargeTypeData: PropTypes.array,
}

export default Form.create()(modal)
